﻿using hvppleDotNet;
using HZH_Controls.Controls;
using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace HangzhouChipProject
{
    /// <summary>
    /// 用以交互各种信号
    /// </summary>
    class InterLayer       
    {
        //获取的图像变量
        public static  HObject hImage =null;
        private static List<HDrawingObject> drawingObjects = new List<HDrawingObject>();
        public static HDrawingObject selected_drawing_object = new HDrawingObject();
        static private XmlDocument xmldoc ;  //创建XML文档对象
        public static object ob = new object();
        HTuple hwindows;
        public InterLayer(HSmartWindowControl hswindows)
        {
            this.hwindows = hswindows.hvppleWindow;
        }
        /// <summary>
        /// 加载外部图像
        /// </summary>
        /// <param name="hSmartWindow"></param>
        /// <returns></returns>
        
        public bool OpenfileDialogPath(HTuple hSmartWindow)
        {
            if (hSmartWindow==null)
            {
                MessageBox.Show("请先选着需要显示的窗口，左键点击窗口即可","图像加载提示框",MessageBoxButtons.OK);
            }
            string filePath;
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "请选择图片";
            fileDialog.Filter = "BMP图像|*.bmp|PNG图像|*.png|JPG图像|*.jpg|所有文件|*.*";
            fileDialog.RestoreDirectory = false;    //若为false，则打开对话框后为上次的目录。若为true，则为初始目录
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath= System.IO.Path.GetFullPath(fileDialog.FileName);
                HOperatorSet.ReadImage(out hImage, filePath);
                PictureProcess.pShowPictureToHwindows(hImage, hSmartWindow);
                fileDialog.Dispose();
                return true;
            }
            else
            {
                fileDialog.Dispose();
                return false;
            }

        }
        /// <summary>
        /// 保存图像
        /// </summary>
        /// <param name="hWindow"></param>
        /// <returns></returns>
        public bool iSavePictrue(HWindow hWindow)
        {
           return  PictureProcess.pSavePictrue(hImage, hWindow);
        }
        public string  DrawRectange1(HSmartWindowControl hsmartwindows)
        {
            
            if (hImage.IsInitialized() == true)
            {
                HDrawingObject rect1 = HDrawingObject.CreateDrawingObject(
                HDrawingObject.HDrawingObjectType.RECTANGLE1, 100, 100, 210, 210);
                rect1.SetDrawingObjectParams("color", "green");
                selected_drawing_object = rect1;
                drawingObjects.Add(rect1);
                hsmartwindows.hvppleWindow.AttachDrawingObjectToWindow(rect1);
                return "已添加矩形1工具";
            }
            else
            {
               return  "请查看图像是否存在";
            }
        }
        public string DrawRectange2(HSmartWindowControl hsmartwindows)
        {
            if (hImage.IsInitialized() == true)
            {
                HDrawingObject rect2 = HDrawingObject.CreateDrawingObject(
                HDrawingObject.HDrawingObjectType.RECTANGLE2, 100, 100, 0, 100, 50);
                rect2.SetDrawingObjectParams("color", "yellow");
                selected_drawing_object = rect2;
                drawingObjects.Add(rect2);
                hsmartwindows.hvppleWindow.AttachDrawingObjectToWindow(rect2);
                return "已添加矩形2工具";
            }
            else
            {
                return "请查看图像是否存在";
            }
        }
        public string DrawCircle(HSmartWindowControl hsmartwindows)
        {
            if (hImage.IsInitialized() == true)
            {
                HDrawingObject circle = HDrawingObject.CreateDrawingObject(
                HDrawingObject.HDrawingObjectType.CIRCLE, 200, 200, 70);
                circle.SetDrawingObjectParams("color", "magenta");
                selected_drawing_object = circle;
                drawingObjects.Add(circle);
                hsmartwindows.hvppleWindow.AttachDrawingObjectToWindow(circle);
                return "已添加圆形工具";
            }
            else
            {
                return "请查看图像是否存在";
            }
        }
        public string SaveROI(HDrawingObject selected_drawing_object,string path)
        {
            if (selected_drawing_object.IsInitialized()==true)
            {
                HObject GetDrawRoI = selected_drawing_object.GetDrawingObjectIconic();
                HOperatorSet.WriteRegion(GetDrawRoI, path);
                return  string.Format("保存完成 :路径 ={0}",path);
            }
            return "ROI不存在，请先绘制";
        }
        public void SetRedColor()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams("color", "red");
        }
        public void SetYellowColor()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams("color", "yellow");
        }
        public void SetGreenColor()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams("color", "green");
        }
        public void SetBlueColor()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams("color", "blue");
        }
        public void Setlinedashed()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams(new HTuple("line_style"), new HTuple(20, 5));

        }
        public void Setlinecontinuous()
        {
            if (selected_drawing_object != null)
                selected_drawing_object.SetDrawingObjectParams(new HTuple("line_style"), new HTuple());

        }
        public static void ClearallTool()
        {
            foreach (HDrawingObject dobj in drawingObjects)
                dobj.Dispose();
            drawingObjects.Clear();
            selected_drawing_object = null;
        }
        public static HObject GetReduceImage()
        {
            if (selected_drawing_object !=null)
            {
                HObject GetDrawRoI = selected_drawing_object.GetDrawingObjectIconic();
                HOperatorSet.ReduceDomain(hImage, GetDrawRoI,out HObject imageaReduce);
                return imageaReduce;
            }
            else
            {
                return null;
            }
         

        }
        public static int  changButtonColor(UCBtnExt button, Color color)
        {
            try
            {
                button.FillColor = color;
                return 0;
            }
            catch (Exception ex)
            {

                return 1;
            }
          
        }
        public static HObject Createmode(HObject himage, HTuple hSmartWindow, HTuple mb_Row, HTuple mb_column,HTuple Rectanglewidth,HTuple Rectanglehigth,HTuple Recinter,int Forrow,int ForColumn)
        {
            HTuple distlen;
            //  HOperatorSet.GetMbutton(hSmartWindow,out HTuple mb_Row,out  HTuple mb_column,out HTuple button);
            HTuple r = mb_Row.D.ToString("0.00");
            HTuple c = mb_column.D.ToString("0.00");
            HOperatorSet.GenCrossContourXld(out HObject hObjectCross, r,c.D,30,0);
            HOperatorSet.GenRectangle1(out HObject hObjectRectangle, mb_Row, mb_column, Rectanglewidth, Rectanglehigth);
            HOperatorSet.GenEmptyRegion(out HObject hObjectRectangle1);
            HOperatorSet.GenEmptyRegion(out HObject hObjectRectanglelist);
            distlen = Rectanglewidth + Recinter;
            for (int i = 0; i < Forrow; i++)
            {
                HOperatorSet.MoveRegion(hObjectRectangle,out HObject hObjectmoveRectangle1,0, distlen);
                HOperatorSet.ConcatObj(hObjectRectangle1, hObjectRectangle,out  hObjectRectangle1);
                HOperatorSet.ConcatObj(hObjectRectangle1, hObjectmoveRectangle1, out hObjectRectangle1);
                distlen +=Rectanglewidth + Recinter;
            }
            return hObjectRectangle1;
        }
        public static void showCross(HObject himage, HTuple hSmartWindow,double x, double y)
        {
            HOperatorSet.ClearWindow(hSmartWindow);
            HOperatorSet.SetColor(hSmartWindow,new HTuple("red"));
            HOperatorSet.GenCrossContourXld(out HObject hObjectCross, x, y, 50, 0);
            HOperatorSet.DispImage(himage, hSmartWindow);
            HOperatorSet.DispObj(hObjectCross, hSmartWindow);
            hObjectCross.Dispose();
        }
      
        /// <summary>
        /// 保存点位运动参数
        /// </summary>
        public static void InitXmlDataParamer()
        {
            if (File.Exists("Axisconfig.xml") && xmldoc == null)
            {
                xmldoc = new XmlDocument();
                xmldoc.Load("Axisconfig.xml");
            }
            //else
            //{
            //    List<AxisConfig> list = new List<AxisConfig>();
            //    list.Add(new AxisConfig(1, 100, 202, 111,50, -287549,34589));
            //    list.Add(new AxisConfig(2, 10, 22, 10, 50, 327293,30000));
            //    list.Add(new AxisConfig(3, 10, 22, 10, 50, 30000,0));
            //    list.Add(new AxisConfig(4, 10, 22, 10, 50, 30000,0));
            //    list.Add(new AxisConfig(5, 100, 202, 111, 50, 32410,30000));
            //    list.Add(new AxisConfig(6, 10, 22, 10, 50, 9084, 30000));
            //    list.Add(new AxisConfig(7, 10, 22, 10, 50, 30000,0));
            //    list.Add(new AxisConfig(8, 10, 22, 10, 50, 30000,0));
            //    xmldoc = new XmlDocument();
            //    XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //    xmldoc.AppendChild(xmldec);
            //    XmlElement axisdata = xmldoc.CreateElement("AxisData");
            //    xmldoc.AppendChild(axisdata);
            //    for (int i = 0; i < list.Count; i++)
            //    {
            //        XmlElement axis = xmldoc.CreateElement("Axis");
            //        axis.SetAttribute("ID", list[i].Id.ToString());
            //        axisdata.AppendChild(axis);
            //        XmlElement vel = xmldoc.CreateElement("Vel");
            //        XmlElement acc = xmldoc.CreateElement("Acc");
            //        XmlElement dec = xmldoc.CreateElement("Dec");
            //        XmlElement smooth = xmldoc.CreateElement("Smooth");
            //        XmlElement poseone = xmldoc.CreateElement("Poseone");
            //        XmlElement posetwo = xmldoc.CreateElement("Posetwo");
            //        vel.InnerText = list[i].Vel.ToString(); ;
            //        acc.InnerText = list[i].Acc.ToString(); ;
            //        dec.InnerText = list[i].Dec.ToString();
            //        smooth.InnerText = list[i].Smooth.ToString(); ;
            //        poseone.InnerText = list[i].Poseone.ToString();
            //        posetwo.InnerText = list[i].Posetwo.ToString();
            //        axis.AppendChild(vel);
            //        axis.AppendChild(acc);
            //        axis.AppendChild(dec);
            //        axis.AppendChild(smooth);
            //        axis.AppendChild(poseone);
            //        axis.AppendChild(posetwo);
            //    }
            //    xmldoc.Save("Axisconfig.xml");
            //}
        
        }
       /// <summary>
       /// 修改点位运动的参数值
       /// </summary>
       /// <param name="axisConfig"></param>
       /// <returns></returns>
        public static bool EditPointsMoveAxisdata(int id,AxisConfig axisConfig)
        {
            bool result = false;
            if (id > 0)
            {
                string xPath = String.Format("/AxisData/Axis[@ID='{0}']", id);
                XmlNode editXmlNode = xmldoc.SelectSingleNode(xPath);
                XmlElement editXmlElement = (XmlElement)editXmlNode;
                if (editXmlElement != null)
                {
                    editXmlElement.SelectSingleNode("Vel").InnerText = axisConfig.Vel.ToString();
                    editXmlElement.SelectSingleNode("Acc").InnerText = axisConfig.Acc.ToString();
                    editXmlElement.SelectSingleNode("Dec").InnerText = axisConfig.Dec.ToString();
                    editXmlElement.SelectSingleNode("Smooth").InnerText = axisConfig.Smooth.ToString();
                    editXmlElement.SelectSingleNode("Poseone").InnerText = axisConfig.Poseone.ToString();
                    editXmlElement.SelectSingleNode("Posetwo").InnerText = axisConfig.Posetwo.ToString();
                    xmldoc.Save("Axisconfig.xml");
                    result = true;
                }
            }
            return result;
        }


        public  struct MyAxisData
        {
            public string Vel;
            public string Acc;
            public string Dec;
            public string Smooth;
            public string Poseone;
            public string Posetwo;
        }
        /// <summary>
        /// 获取轴号的参数配置
        /// </summary>
        /// <param name="nodelist"></param>
        /// <param name="listnode"></param>
        /// <returns></returns>
        public static bool GetNodeInnerText( int Id,ref MyAxisData myAxisData)
        {
            lock (ob)
            {


                InitXmlDataParamer();
                string xPath = String.Format("/AxisData/Axis[@ID='{0}']", Id);
                XmlNode editXmlNode = xmldoc.SelectSingleNode(xPath);
                XmlElement editXmlElement = (XmlElement)editXmlNode;
                if (editXmlElement != null)
                {
                    myAxisData.Vel = editXmlElement.SelectSingleNode("Vel").InnerText;
                    myAxisData.Acc = editXmlElement.SelectSingleNode("Acc").InnerText;
                    myAxisData.Dec = editXmlElement.SelectSingleNode("Dec").InnerText;
                    myAxisData.Smooth = editXmlElement.SelectSingleNode("Smooth").InnerText;
                    myAxisData.Poseone = editXmlElement.SelectSingleNode("Poseone").InnerText;
                    myAxisData.Posetwo = editXmlElement.SelectSingleNode("Posetwo").InnerText;
                    return true;
                }
                return false;
            }
        }

        public static MyAxisData GetNodeInnerText(int Id)
        {
            InitXmlDataParamer();
            string xPath = String.Format("/AxisData/Axis[@ID='{0}']", Id);
            XmlNode editXmlNode = xmldoc.SelectSingleNode(xPath);
            XmlElement editXmlElement = (XmlElement)editXmlNode;

            MyAxisData myAxisData = new MyAxisData();
            if (editXmlElement != null)
            {
                myAxisData.Vel = editXmlElement.SelectSingleNode("Vel").InnerText;
                myAxisData.Acc = editXmlElement.SelectSingleNode("Acc").InnerText;
                myAxisData.Dec = editXmlElement.SelectSingleNode("Dec").InnerText;
                myAxisData.Smooth = editXmlElement.SelectSingleNode("Smooth").InnerText;
                myAxisData.Poseone = editXmlElement.SelectSingleNode("Poseone").InnerText;
                myAxisData.Posetwo = editXmlElement.SelectSingleNode("Posetwo").InnerText;
                //return myAxisData;
            }
            return myAxisData;
        }
    }
}
